var cmApp, CURRENTUSER;

var pwdWin = new Bee.Window({
    id: 'main-pwdWin',
    title: '修改密码',
    width: 240,
    height: 160,
    draggable: false,
    resizable: false,
    items: new Bee.form.DWRForm({
        dwr: {
            load: Ext.emptyFn,
            save: function(values){
                smDwr.updatePwd(CURRENTUSER.sid, values.pwd, values.prePwd, function(result){
                    if (result.flag && result.flag == 'true') {
                        smDwr.doLogout(function(){
                            pwdWin.hide();
                            Ext.Msg.alert('提示', '密码修改成功，请以新的密码登录系统！', function(){
                                location.reload();
                                if (window.opener) {
                                    window.opener.close();
                                }
                            });
                        });
                    }
                    else {
                        Bee.msg(result.msg);
                        pwdWin.getBasicForm().reset();
                        pwdWin.getBasicForm().isValid();
                    }
                    pwdWin.getForm().afterRemote();
                });
            }
        },
        items: [{
            fieldLabel: '当前密码',
            name: 'prePwd',
            allowBlank: false,
            inputType: 'password',
            minLength: 6,
            maxLength: 20
        }, {
            fieldLabel: '新密码',
            name: 'pwd',
            allowBlank: false,
            inputType: 'password',
            minLength: 6,
            maxLength: 20
        }, {
            fieldLabel: '重复密码',
            name: 'rePwd',
            allowBlank: false,
            inputType: 'password',
            minLength: 6,
            maxLength: 20,
            validator: function(val){
                if (pwdWin.getBasicForm().getValues().pwd != val) {
                    return '两次输入的密码不一致！';
                }
                else {
                    return true;
                }
            }
        }]
    })
});

function openT(a, b){
    Bee.openTab({
        attributes: {
            cname: a,
            link: b,
            leaf: 1
        }
    });
};

function initFun(){
    var welcome = new Ext.Panel({
        id: 'main-viewport-welcome',
        title: '首页',
        layout: 'fit'
    });
    
    Ext.Ajax.request({
        method: 'GET',
        url: 'modules/welcome.js',
        success: function(response){
            var module = eval(response.responseText);
            new module(welcome);
            doInit(welcome);
        }
    });
    
    var doInit = function(welcomeTab){
        if (!Bee.hasLogout) {
            Ext.fly('modifyPwdSpan').setStyle({
                display: 'none'
            });
            Ext.fly('logoutSpan').setStyle({
                display: 'none'
            });
        }
        
        Ext.get('modiinfo').update(CURRENTUSER.cname);
        
        var north = new Ext.BoxComponent({
            id: 'main-viewport-north',
            region: 'north',
            el: 'north',
            height: 30,
            border: false
        });
        
        var west = new Ext.tree.TreePanel({
            id: 'main-viewport-west',
            title: '系统菜单',
            region: 'west',
            hideCollapseTool: true,
            titleCollapse: false,
            collapseMode: 'mini',
            split: true,
            rootVisible: false,
            useArrows: true,
            animCollapse: false,
            stateful: false,
            width: 150,
            autoScroll: true,
            collapsible: true,
            margins: '3 0 3 3',
            minWidth: 150,
            maxWidth: 150,
            loader: new Bee.tree.DWRTreeLoader({
                dwrCall: smDwr.findMenuEx,
                nodeMeta: {
                    id: 'sid',
                    text: 'cname',
                    leaf: 'isLeaf'
                }
            }),
            root: {
                nodeType: 'async',
                text: '系统菜单',
                draggable: false,
                id: '0',
                sid: '0',
                cname: '系统菜单',
                leaf: false
            },
            listeners: {
                'click': Bee.openTab
            }
        });
        
        var center = new Ext.TabPanel({
            id: 'main-viewport-center',
            region: 'center',
            contentEl: 'center',
            activeTab: 0,
            tabWidth: 135,
            minTabWidth: 135,
            margins: '3 3 3 0',
            enableTabScroll: true,
            monitorResize: true,
            deferredRender: false,
            items: [welcomeTab]
        });
        
        cmApp = new Bee.app(north, west, center);
        
        Ext.get('loading-mask').fadeOut({
            duration: 0.2,
            remove: true
        });
        
        Ext.get('modifyPwd').on('click', function(){
            pwdWin.show();
        });
        
        Ext.get('logout').on('click', function(){
            Ext.Msg.confirm('确认', '您确定要退出本系统吗？', function(para){
                if (para == 'yes') {
                    smDwr.doLogout(function(){
                        location.reload();
                        if (window.opener) {
                            window.opener.close();
                        }
                    });
                }
            });
        });
    };
}

Ext.onReady(function(){
    Ext.QuickTips.init();
    smDwr.getCurrentUser({
        callback: function(user){
            Ext.get('loading').fadeOut({
                duration: 0.2,
                remove: true
            });
            if (user.sid !== undefined && user.sid !== null) {
                CURRENTUSER = user;
                initFun();
            }
            else {
                location.replace('login.html');
            }
        }
    });
});
